package com.jianda.view;

import com.jianda.commons.content.PopContent;
import com.jianda.commons.util.SerialPortUtils;
import com.jianda.entity.dto.SerialPostDto;
import com.jianda.view.windows.Mainwindows;

import javax.swing.*;
import java.util.Map;

/**
 * 按钮设置
 */
public class ButtonView {

    /**
     * 串口窗口按钮
     * @param jFrame
     */
    public static void editTextSerial(JFrame jFrame) {

        JFrame frame = Mainwindows.getInstance();
        int x = (int)Math.abs(jFrame.getBounds().getWidth() * 0.51);
        int y = (int)Math.abs(jFrame.getBounds().getHeight() * 0.748);
        int width = (int)Math.abs(jFrame.getBounds().getWidth() * 0.20);
        int height = (int)Math.abs(jFrame.getBounds().getHeight() * 0.084);

        // 取消按钮
        JButton cancelButton = new JButton("取消");
        cancelButton.setBounds(x, y, width, height);
        jFrame.add(cancelButton);
        cancelButton.addActionListener((ActionListener) -> {
            frame.setEnabled(true);
            jFrame.setVisible(false);
        });

        System.out.println("EditSerial-X:" + (int) jFrame.getBounds().getX());
        System.out.println("EditSerial-Y:" + (int) jFrame.getBounds().getY());
        System.out.println("EditSerial-W:" + (int) jFrame.getBounds().width);
        System.out.println("EditSerial-H:" + (int) jFrame.getBounds().height);

        // 确定按钮
        JButton sureButton = new JButton("确定");
       // x = jFrame.getWidth() == 0 ? 1778 : jFrame.getWidth() - 90;
        x = (int)Math.abs(jFrame.getBounds().getWidth() * 0.730);
        SerialPostDto serialPost = SerialPostDto.getInstance();
       // sureButton.setBounds(x, jFrame.getHeight() - 90, 60, 30);
        sureButton.setBounds(x, y, width, height);
        jFrame.add(sureButton);
        sureButton.addActionListener((ActionListener) -> {
            // 赋值
            valuation(serialPost);
            // 重置串口连接
            SerialPortUtils serialPort = SerialPortUtils.getSerialPortUtilsInstance();
            serialPort.closeSerialPort();
            // 串口连接对象
            SerialPostDto serialPostDto = SerialPostDto.getInstance();
            // 开始连接
            try {
                serialPort.init(serialPostDto);
            } catch (Exception e) {
                e.printStackTrace();
                PopContent.popContent(2, e.getMessage());
                return;
            }
            frame.setEnabled(true);
            jFrame.setVisible(false);
        });
    }

    private static void valuation(SerialPostDto serialPost) {
        Map<String, String> serialMap = TextView.getSerialMap();
        String serialNumber = serialMap.get("serialNumber");
        String baudRateString = serialMap.get("baudRate");
        String checkoutBitString = serialMap.get("checkoutBit");
        String dataBitString = serialMap.get("dataBit");
        String stopBitString = serialMap.get("stopBit");
        int baudRate = 0;
        int checkoutBit = 0;
        int dataBit = 0;
        int stopBit = 0;
        try {
            baudRate = Integer.parseInt(baudRateString);
            checkoutBit = Integer.parseInt(checkoutBitString);
            dataBit = Integer.parseInt(dataBitString);
            stopBit = Integer.parseInt(stopBitString);
        } catch (Exception e) {
            e.printStackTrace();
          //  PopContent.popContent(1, "请输入数字");
            System.out.println("类型转换异常，请输入数字类型");
        }

        serialPost.setSerialNumber(serialNumber == null ? "": serialNumber);
        serialPost.setBaudRate(baudRate);
        serialPost.setCheckoutBit(checkoutBit);
        serialPost.setDataBit(dataBit);
        serialPost.setStopBit(stopBit);
    }
}
